/**
 * @file 类型相关api
 */

/**
 * 判断传入数据是否为 null
 * @param value
 * @returns {boolean}
 */
export const isNull = value => Object.prototype.toString.call(value) === '[object Null]'

/**
 * 判断传入数据是否为 undefined
 * @param value
 * @returns {boolean}
 */
export const isUndefined = value => Object.prototype.toString.call(value) === '[object Undefined]'

/**
 * 判断传入数据是否为 Number
 * @param value
 * @returns {boolean}
 */
export const isNumber = value => Object.prototype.toString.call(value) === '[object Number]'

/**
 * 判断传入数据是否为 Boolean
 * @param value
 * @returns {boolean}
 */
export const isBoolean = value => Object.prototype.toString.call(value) === '[object Boolean]'

/**
 * 判断传入数据是否为 String
 * @param value
 * @returns {boolean}
 */
export const isString = value => Object.prototype.toString.call(value) === '[object String]'

/**
 * 判断传入数据是否为 Object
 * @param value
 * @returns {boolean}
 */
export const isObject = value => Object.prototype.toString.call(value) === '[object Object]'

/**
 * 判断传入数据是否为 Symbol
 * @param value
 * @returns {boolean}
 */
export const isSymbol = value => Object.prototype.toString.call(value) === '[object Symbol]'

/**
 * 判断传入数据是否为 Array
 * @param value
 * @returns {boolean}
 */
export const isArray = value => Object.prototype.toString.call(value) === '[object Array]'

/**
 * 判断传入数据是否为 Function
 * @param value
 * @returns {boolean}
 */
export const isFunction = value => Object.prototype.toString.call(value) === '[object Function]'

/**
 * 判断传入数据是否为 Date
 * @param value
 * @returns {boolean}
 */
export const isDate = value => Object.prototype.toString.call(value) === '[object Date]'

/**
 * 判断传入数据是否为 Math
 * @param value
 * @returns {boolean}
 */
export const isMath = value => Object.prototype.toString.call(value) === '[object Math]'

/**
 * 判断传入数据是否为 Set
 * @param value
 * @returns {boolean}
 */
export const isSet = value => Object.prototype.toString.call(value) === '[object Set]'

/**
 * 判断传入数据是否为 WeakSet
 * @param value
 * @returns {boolean}
 */
export const isWeakSet = value => Object.prototype.toString.call(value) === '[object WeakSet]'
/**
 * 判断传入数据是否为 Map
 * @param value
 * @returns {boolean}
 */
export const isMap = value => Object.prototype.toString.call(value) === '[object Map]'

/**
 * 判断传入数据是否为 WeakMap
 * @param value
 * @returns {boolean}
 */
export const isWeakMap = value => Object.prototype.toString.call(value) === '[object WeakMap]'

/**
 * 获取传入数据的类型
 * @example getType(null) // => "null"
 * @param {any} value
 * @returns {String} 返回传入数据所属类型的小写
 */
export const getType = value =>
  Object.prototype.toString
    .call(value)
    .match(/\s([a-z]+)/i)[1]
    .toLocaleLowerCase()

/**
 * 判断传入数据是否为空，包含 undefined、null、空字符串（只有空格的字符串）、空数组、空对象
 * @example isEmpty('   ') // => true
 * @param {*} value 要判断的数据
 * @returns {boolean}
 */
export const isEmpty = value => {
  if (
    isUndefined(value) ||
    isNull(value) ||
    (isString(value) && value.replace(/(^\s*)|(\s*$)/g, '').length === 0) ||
    (isObject(value) && !Object.keys(value).length) ||
    (isArray(value) && !value.length)
  ) {
    return true
  }
  return false
}

/**
 * 判断值是否不为空，与isEmpty取反
 * @param {unknown} value 要判断的数据
 * @returns {boolean}
 *
 * @example
 * isNotEmpty(0) // => true
 * isNotEmpty('   ') // => false
 * isNotEmpty({}) // => false
 */
export const isNotEmpty = value => !isEmpty(value)
